package com.aijk.web.admin.controller.sem;

import com.aijk.commons.lang.utils.StringUtil;
import com.aijk.commons.mijo.utils.PojoUtil;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.api.*;
import com.aijk.ehm.model.*;
import com.aijk.ehm.model.qo.EhmResidentQo;
import com.aijk.ehm.model.qo.SemRecordQo;
import com.aijk.ehm.model.vo.EhmResidentVo;
import com.aijk.web.admin.RetMsg;
import com.aijk.web.admin.controller.BaseController;
import com.aijk.web.admin.model.SemRecordExt;
import com.aijk.web.admin.model.SemShow;
import com.aijk.web.admin.utils.DataTableUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 健康监测
 */
@Controller
@RequestMapping(value = "/admin/semRecord")
public class SemRecordController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(SemRecordController.class);

    @Autowired
    private SemRecordApi semRecordApi;

    @Autowired
    private EhmResidentApi ehmResidentApi;

    @Autowired
    private SemBpApi semBpApi;

    @Autowired
    private SemStepApi semStepApi;

    @Autowired
    private SemHeightApi semHeightApi;

    @Autowired
    private SemWeightApi semWeightApi;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return "sem/list";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg list(@RequestParam HashMap skey, @RequestParam(value = "draw", defaultValue = "1") Long draw) {
        Long iTotalRecords;
        DataTableUtil.FormatOptions(skey);
        if (Utils.isNotEmpty(skey.get("orderbyclause"))) {
            skey.put("orderbyclause", StringUtil.toUnderlineCase(Utils.toString(skey.get("orderbyclause"))));
        }
        draw += 1;
        SemRecordQo qo = DataTableUtil.fillQo(skey, SemRecordQo.class);
        if (Utils.isNotEmpty(skey.get("title"))) {
            EhmResidentQo ehmResidentQo = new EhmResidentQo();
            ehmResidentQo.setTitle((String) skey.get("title"));
            List<EhmResidentVo> residents = ehmResidentApi.list(ehmResidentQo);
            if (residents!= null && residents.size()>0){
                List<Long> ids = new ArrayList<>();
                for (EhmResidentVo ehmResidentVo:residents){
                    ids.add(ehmResidentVo.getId());
                }
                qo.setResidentIds(ids);
            }else {
                List<Long> ids = new ArrayList<>();
                ids.add(-1l);
                qo.setResidentIds(ids);
            }
        }
        if(isSubCompany()){
            qo.setCompanyId(getCurrentUser().getCompanyId());
        }
        qo.setStatus((byte) 1);
        PageInfo<SemRecord> pageInfo = semRecordApi.page(qo);

        List<SemRecordExt> list = new ArrayList<>();
        List<SemRecord> records = pageInfo.getList();
        if (records != null && records.size() > 0) {
            list = PojoUtil.convert(records, SemRecordExt.class);
            for (SemRecordExt recordExt : list) {

                if (recordExt.getResidentId()> 0){
                    EhmResident ehmResident = ehmResidentApi.findOne(recordExt.getResidentId());
                    recordExt.setName(ehmResident.getTitle());
                }
                if (Utils.isNotEmpty(recordExt.getDvtype()) && recordExt.getDataId()>0){
                    Long dataid = recordExt.getDataId();
                    switch (recordExt.getDvtype()){
                        case "1":
                            SemBp semBp = semBpApi.findOne(dataid);
                            recordExt.setDvsource(semBp.getDvsource());
                            recordExt.setLx("血压");
                            recordExt.setResult(semBp.getSbp() + "/" + semBp.getDbp()+ "" + semBp.getUnits() + "(收缩压/舒张压)");
                            break;
                        case "2":
                            SemHeight semHeight = semHeightApi.findOne(dataid);
                            recordExt.setDvsource(semHeight.getDvsource());
                            recordExt.setLx("身高");
                            recordExt.setResult(semHeight.getHeight() + "" + semHeight.getUnits() + "(身高)");
                            break;
                        case "3":
                            SemWeight semWeight = semWeightApi.findOne(dataid);
                            recordExt.setDvsource(semWeight.getDvsource());
                            recordExt.setLx("体重");
                            recordExt.setResult(semWeight.getWeight() + "" + semWeight.getUnits() + "(体重)");
                            break;
                        case "4":
                            SemStep semStep = semStepApi.findOne(dataid);
                            recordExt.setDvsource(semStep.getDvsource());
                            recordExt.setLx("记步");
                            recordExt.setResult(semStep.getStepNum() + "(步数)");
                            break;
                        default:break;
                    }
                }
            }
        }

        iTotalRecords = pageInfo.getTotal();
        RetMsg retMsg = new RetMsg();
        retMsg.put("data", list);
        retMsg.put("draw", draw);
        retMsg.put("recordsTotal", iTotalRecords);
        retMsg.put("recordsFiltered", iTotalRecords);
        return retMsg;
    }

    @RequestMapping(value = "/sem/list", method = RequestMethod.GET)
     public String semlist(@RequestParam(value = "id") Long id,@RequestParam(value = "dvtype") String dvtype, Model model) {
        model.addAttribute("mid",id);
        model.addAttribute("dvtype",dvtype);

        return "sem/record_list";
    }

    @RequestMapping(value = "/resident/list", method = RequestMethod.GET)
    public String reslist(@RequestParam(value = "id") Long id, Model model) {
        model.addAttribute("residentId",id);
        if(isSubCompany()){
            model.addAttribute("sys_flag",true);
        }else{
            model.addAttribute("sys_flag",false);
        }
        return "ehm/user/sem_list";
    }

    @RequestMapping(value = "/sem/list", method = RequestMethod.POST)
    @ResponseBody
    public RetMsg semlist(@RequestParam HashMap skey, @RequestParam(value = "draw", defaultValue = "1") Integer draw) {
        Long iTotalRecords = 1l;
        DataTableUtil.FormatOptions(skey);
        String dvtype = Utils.toString(skey.get("dvtype"));
        Long dataid = Utils.toLong(skey.get("mid"));
        List<SemShow> list = new ArrayList<>();
        if (Utils.isNotEmpty(dvtype) && dataid > 0){
            SemShow semShow = new SemShow();
            SemShow semShow1 = new SemShow();
            switch (dvtype){
                case "1":
                    SemBp semBp = semBpApi.findOne(dataid);
                    semShow.setTitle("收缩压");
                    semShow.setResult(String.valueOf(semBp.getSbp()));
                    semShow.setRrs(semBp.getSbpRrs());
                    semShow.setTs(semBp.getSbpResult());
                    semShow.setUntis(semBp.getUnits());
                    semShow1.setTitle("舒张压");
                    semShow1.setResult(String.valueOf(semBp.getDbp()));
                    semShow1.setRrs(semBp.getDbpRrs());
                    semShow1.setTs(semBp.getDbpResult());
                    semShow1.setUntis(semBp.getUnits());
                    list.add(semShow);
                    list.add(semShow1);
                    iTotalRecords = 2l;
                    break;
                case "2":
                    SemHeight semHeight = semHeightApi.findOne(dataid);
                    semShow.setTitle("身高");
                    semShow.setResult(String.valueOf(semHeight.getHeight()));
                    semShow.setUntis(semHeight.getUnits());
                    list.add(semShow);
                    break;
                case "3":
                    SemWeight semWeight = semWeightApi.findOne(dataid);
                    semShow.setTitle("体重");
                    semShow.setResult(String.valueOf(semWeight.getWeight()));
                    semShow.setUntis(semWeight.getUnits());
                    list.add(semShow);
                    break;
                case "4":
                    SemStep semStep = semStepApi.findOne(dataid);
                    semShow.setTitle("记步");
                    semShow.setResult(String.valueOf(semStep.getStepNum()));
                    semShow.setUntis(semStep.getUnits());
                    list.add(semShow);
                    break;
                default:break;
            }
        }

        RetMsg regMsg = new RetMsg();

        regMsg.put("data", list);
        regMsg.put("draw", draw);
        regMsg.put("recordsTotal", iTotalRecords);
        regMsg.put("recordsFiltered", iTotalRecords);

        return regMsg;
    }


}